<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');
	
/**
 * 后台Api调用类
 * Dayrui Website Management System
 *
 * @since		version 2.5.0
 * @author		Dayrui <dayrui@gmail.com>
 * @license     http://www.dayrui.com/license
 * @copyright   Copyright (c) 2011 - 9999, Dayrui.Com, Inc.
 * @filesource	svn://www.dayrui.net/v2/dayrui/controllers/admin/api.php
 */
 
class Api extends M_Controller {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
    }

    /**
     * ajax文件上传
     *
     * @return void
     */
	public function ajax_upload() {
		
		$ext = $this->input->get('ext');
		$file = $this->input->get('file');
		$path = $this->input->get('path');
		$name = $this->input->get('name');
		if (!is_dir(FCPATH.$path)) {
			exit(dr_json(0, '目录（'.$path.'）不存在'));
		}
		$this->load->library('upload', array(
			'max_size' => 1024 * 1024,
			'overwrite' => TRUE,
			'file_name' => $file,
			'upload_path' => FCPATH.$path,
			'allowed_types' => $ext ? $ext : '*',
			'file_ext_tolower' => TRUE,
		));
		if ($this->upload->do_upload($name)) {
			$info = $this->upload->data();
			exit(dr_json(1, $info['orig_name']));
		} else {
			exit(dr_json(0, $this->upload->display_errors('', '')));
		}
	}
	
	
    /**
     * 文件上传
     *
     * @return void
     */
    public function upload() {
        $ext = 'jpg,gif,png,js,css,html,swf,zip';
        $this->template->assign(array(
            'ext' => str_replace(',', '|', $ext),
            'page' => 0,
            'size' => 1024 * 1024,
            'path' => $this->input->get('path'),
            'types' => '*.'.str_replace(',', ';*.', $ext),
            'fcount' => 50,
            'is_admin' => 1,
        ));
        $this->template->display('upload.html');
    }

    /**
     * 文件上传处理
     *
     * @return void
     */
    public function swfupload() {

        if (IS_POST) {
            $ext = 'jpg,gif,png,js,css,html,swf,zip';
            $path = $this->input->post('path');
            if (!is_dir($path)) {
                exit('0,目录（'.$path.'）不存在');
            }
            $this->load->library('upload', array(
                'max_size' => 1024 * 1024,
                'overwrite' => TRUE,
                'file_name' => '',
                'upload_path' => $path,
                'allowed_types' => str_replace(',', '|', $ext),
                'file_ext_tolower' => TRUE,
            ));
            if ($this->upload->do_upload('Filedata')) {
                $info = $this->upload->data();
                $_ext = str_replace('.', '', $info['file_ext']);
                $file = str_replace(FCPATH, '', $info['full_path']);
                if (!is_file(FCPATH.$file)) {
                    $file = SITE_URL.'dayrui/statics/images/ext/blank.gif';
                }
                $icon = is_file(FCPATH.'dayrui/statics/images/ext/'.$_ext.'.gif') ? SITE_URL.'dayrui/statics/images/ext/'.$_ext.'.gif' : SITE_URL.'dayrui/statics/images/ext/blank.gif';
                //唯一ID,文件全路径,图标,文件名称,文件大小,扩展名
                exit('1,'.$file.','.$icon.','.str_replace(array('|', '.'.$_ext), '', $info['client_name']).','.dr_format_file_size($info['file_size'] * 1024).','.$_ext);
            } else {
                exit('0,'.$this->upload->display_errors('', ''));
            }
        }
    }
	
	/**
     * 查看资料
     */
	public function member() {

        $uid = str_replace('author_', '', $this->input->get('uid'));
        if ($uid == 'guest' || !$uid) {
            exit('<div style="padding-top:50px;color:blue;font-size:14px;text-align:center">'.lang('guest').'</div>');
        }
        if (is_numeric($uid)) {
            $data = $this->db->where('uid', (int)$uid)->limit(1)->get('member')->row_array();
        } else {
            $data = $this->db->where('username', $uid)->limit(1)->get('member')->row_array();
        }

		if (!$data) {
            exit('(#'.$uid.')'.lang('236'));
        }

        $this->load->library('dip');
        $data['address'] = $this->dip->address($data['regip']);

		$this->template->assign(array(
			'data' => $data,
		));
		$this->template->display('member.html');
	}
	
	/**
     * 测试ftp链接状态
     */
	public function testftp() {
	
		$rurl = $this->input->get('rurl');
		$host = $this->input->get('host');
		$port = $this->input->get('port');
		$pasv = $this->input->get('pasv');
		$path = $this->input->get('path');
		$mode = $this->input->get('mode');
        $username = $this->input->get('username');
        $password = $this->input->get('password');
		
		if (!$host || !$username || !$password) {
            exit(lang('035'));
        }

        if (!$rurl) {
            exit(lang('199'));
        }

		$this->load->library('ftp');
		if (!$this->ftp->connect(array(
			'hostname' => $host,
			'username' => $username,
			'password' => $password,
			'port' => $port ? $prot : 21,
			'passive' => $pasv ? TRUE : FALSE,
			'debug' => FALSE
		))) {
            exit(lang('036'));
        }

		if (!$this->ftp->upload(FCPATH.'index.php', $path.'/test.ftp', $mode, 0775)) {
            exit(lang('037'));
        }

        if (strpos(dr_catcher_data($rurl.'/test.ftp'), 'dayrui.com') === FALSE) {
            exit(lang('200'));
        }

		if (!$this->ftp->delete_file($path.'/test.ftp')) {
            exit(lang('039'));
        }

		$this->ftp->close();
		
		exit('ok');
	}

    // 测试阿里云存储状态
    public function aliyuntest() {

        $id = $this->input->get('id');
        $host = $this->input->get('host');
        $rurl = $this->input->get('rurl');
        $secret = $this->input->get('secret');
        $bucket = $this->input->get('bucket');

        if (!$id || !$host || !$secret || !$bucket) {
            exit(lang('035'));
        }

        if (!$rurl) {
            exit(lang('199'));
        }

        require_once FCPATH.'dayrui/libraries/AliyunOSS/sdk.class.php';
        $oss = new ALIOSS($id, $secret, $host);
        $response = $oss->upload_file_by_file($bucket, 'test.txt', FCPATH.'index.php');

        if ($response->status == 200) {
            if (strpos(dr_catcher_data($rurl.'/test.txt'), 'dayrui.com') === FALSE) {
                $oss->delete_object($bucket, 'test.txt');
                exit(lang('200').''.$rurl.'/test.txt');
            }
            exit('ok');
        } else {
            exit($response->body);
        }

    }

    public function login() {

        $uid = (int)$this->session->userdata('uid');
        $admin = (int)$this->session->userdata('admin');
        if ($this->uid == FALSE
            || $uid != $this->uid
            || $admin != $uid) {
            $this->template->display('login_ajax.html');
        } else {
            $data = $this->member_model->get_admin_member($this->uid, 1);
            if ($data) {
                ob_clean();
                exit('ok');
            } else {
                $this->template->display('login_ajax.html');
            }
        }

    }

    // 测试百度云存储状态
    public function baidutest() {

        $ak = $this->input->get('ak');
        $sk = $this->input->get('sk');
        $host = $this->input->get('host');
        $rurl = $this->input->get('rurl');
        $bucket = $this->input->get('bucket');

        if (!$ak || !$host || !$sk || !$bucket) {
            exit(lang('035'));
        }

        if (!$rurl) {
            exit(lang('199'));
        }

        require_once FCPATH.'dayrui/libraries/BaiduBCS/bcs.class.php';
        $bcs = new BaiduBCS($ak, $sk, $host);
        $opt = array();
        $opt['acl'] = BaiduBCS::BCS_SDK_ACL_TYPE_PUBLIC_WRITE;
        $opt['curlopts'] = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 1800);
        $response = $bcs->create_object($bucket, '/test.txt', FCPATH.'index.php', $opt);

        if ($response->status == 200) {
            if (strpos(dr_catcher_data($rurl.'/test.txt'), 'dayrui.com') === FALSE) {
                exit(lang('200'));
            }
            $bcs->delete_object($bucket, '/test.txt');
            exit('ok');
        } else {
            exit('error');
        }

    }
}